using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data.OleDb;

/// <summary>
/// Summary description for BdcWssEditFieldType
/// </summary>
namespace Siderys.Blog.CustomField
{
    public class BdcWssEditFieldType : UserControl, IFieldEditor
    {
        protected TextBox txtConnectionString = null;
        protected Button cmdTestConnection = null;
        protected Label lblTestConnection = null;
        protected TextBox txtSelect = null;
        protected Button cmdTestSelect = null;
        protected Label lblTestSelect = null;
        protected DropDownList ddlSelectResult = null;
        private OleDbConnection mConnection = null;
        private OleDbCommand mCommand = null;
        private OleDbDataReader mReader = null;

        public BdcWssEditFieldType()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            //agrego el metodo para manejar el evento click
            this.cmdTestConnection.Click += new EventHandler(cmdTestConnection_Click);
            //agrego el metodo para manejar el evento click
            this.cmdTestSelect.Click += new EventHandler(cmdTestSelect_Click);
        }

        #region IFieldEditor Members

        public bool DisplayAsNewSection
        {
            get
            {
                return true;
            }
        }

        public void InitializeWithField(SPField field)
        {
            BdcWss lBdc = (BdcWss)field;
            if (lBdc != null)
            {
                EnsureChildControls();
                string lStringConnection = lBdc.GetCustomProperty("StringConnection").ToString();
                string lStringSelect = lBdc.GetCustomProperty("StringSelect").ToString();

                txtConnectionString.Text = lStringConnection;
                txtSelect.Text = lStringSelect;
            }
        }

        public void OnSaveChange(SPField field, bool isNewField)
        {
            BdcWss lBdcWss = (BdcWss)field;
            if (isNewField)
            {
                lBdcWss.UpdateConnectionStringProperty(this.txtConnectionString.Text);
                lBdcWss.UpdateSelectStringProperty(this.txtSelect.Text);
            }
            else
            {
                //Almaceno la cadena de conexion.
                lBdcWss.ConeectionString = this.txtConnectionString.Text;
                //Almaceno la consulta Select.
                lBdcWss.SelectString = this.txtSelect.Text;
            }
        }

        #endregion

        protected void cmdTestConnection_Click(object sender, EventArgs e)
        {
            try
            {
                mConnection = new OleDbConnection(this.txtConnectionString.Text);
                mConnection.Open();
                lblTestConnection.Text = "Se conecto correctamente";
            }
            catch (OleDbException ex)
            {
                lblTestConnection.Text = ex.Message;
            }
            finally
            {
                mConnection.Close();
            }
        }

        protected void cmdTestSelect_Click(object sender, EventArgs e)
        {
            try
            {
                mConnection = new OleDbConnection(this.txtConnectionString.Text);
                mConnection.Open();
                mCommand = new OleDbCommand(this.txtSelect.Text, mConnection);
                mReader = mCommand.ExecuteReader();
                ddlSelectResult.Visible = true;
                ddlSelectResult.Items.Clear();
                while (mReader.Read())
                {
                    ddlSelectResult.Items.Add(new ListItem(mReader.GetString(0)));
                }
                lblTestSelect.Text = "Se ejecuto correctamente";
            }
            catch (OleDbException ex)
            {
                lblTestSelect.Text = ex.Message;
            }
            finally
            {
                mReader.Close();
                mConnection.Close();
            }
        }
    }
}